﻿using Hangfire;
using Hangfire.HttpJob;
using Hangfire.MySql;
using QuickCode.Job.HangFire;
using System.Transactions;

namespace QuickCode.Job.HangFire
{
    public static class StorageType
    {
        public static void UseStorage(this IServiceCollection services, string connectstring)
        {
            // Add Hangfire services.
            services.AddHangfire(configuration => configuration
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            //.UseSqlServerStorage(connectstring, new SqlServerStorageOptions
            //{
            //   CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
            //   SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
            //   QueuePollInterval = TimeSpan.Zero,
            //   UseRecommendedIsolationLevel = true,
            //   UsePageLocksOnDequeue = true,
            //   DisableGlobalLocks = true,
            //}));
            .UseStorage(new MySqlStorage(
                connectstring,
                new MySqlStorageOptions
                {
                    TransactionIsolationLevel = IsolationLevel.ReadCommitted,
                    QueuePollInterval = TimeSpan.FromSeconds(15),
                    JobExpirationCheckInterval = TimeSpan.FromHours(1),
                    CountersAggregateInterval = TimeSpan.FromMinutes(5),
                    PrepareSchemaIfNecessary = true,
                    DashboardJobListLimit = 50000,
                    TransactionTimeout = TimeSpan.FromMinutes(1),
                    TablesPrefix = "Hangfire"
                })).UseHangfireHttpJob());
        }
    }
}
